import { Alova } from '@/utils/http/alova/index';

// 定义组织机构数据模型接口
export interface OrgItem {
  id: string;
  orgName: string;
  orgCode: string;
  orgType: string;
  parentId: string;
  parentName: string;
  sort: number;
  remark: string;
  createTime: string;
  createName: string;
  createId: string;
  updateTime?: string;
  updateName?: string;
  updateId?: string;
  children?: OrgItem[];
}

export interface OrgParams {
  orgName?: string;
  orgCode?: string;
  orgType?: string;
  parentId?: string;
}

export interface OrgTreeItem {
  id: string;
  label: string;
  key: string;
  children?: OrgTreeItem[];
}

// 定义通用响应结构
export interface ApiResponse<T> {
  code: number;
  sucMsg: string;
  errMsg: string;
  encrypt: boolean;
  data: T;
}

export const baseApi = '/sys/org';

/**
 * @description: 获取组织机构树形结构
 */
export function getOrgTree() {
  return Alova.Get<ApiResponse<OrgTreeItem[]>>(`${baseApi}/memberTree`);
}

/**
 * @description: 新增组织机构
 */
export function addOrg(params: any) {
  return Alova.Post<ApiResponse<boolean>>(`${baseApi}/add`, { params });
}

/**
 * @description: 更新组织机构
 */
export function updateOrg(params: any) {
  return Alova.Post<ApiResponse<boolean>>(`${baseApi}/update`, { params });
}

/**
 * @description: 删除组织机构
 */
export function deleteOrg(params: { id: string }) {
  return Alova.Post<ApiResponse<boolean>>(`${baseApi}/delete`, { params });
}

/**
 * @description: 获取组织机构列表（分页）
 */
export function getOrgList(params: {
  currentPage: number;
  pageSize: number;
  orgName?: string;
  orgCode?: string;
}) {
  return Alova.Post<
    ApiResponse<{
      records: OrgItem[];
      total: number;
      size: number;
      current: number;
      pages: number;
    }>
  >(`${baseApi}/page`, { params });
}

/**
 * @description: 获取所有组织机构（用于下拉选择）
 */
export function getAllOrgs() {
  return Alova.Get<ApiResponse<OrgItem[]>>(`${baseApi}/all`);
}
